package cn.com.cennavi.test;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.nio.Buffer;

import org.jboss.netty.handler.queue.BufferedWriteHandler;

/**
 * 轨迹配退失败点验证
 * 
 * @ClassName: ErrorDataCheckOut
 * @Description: TODO（这里用一句话描述这个类的作用）
 * @author bull
 * @date 2016年5月12日 下午1:55:14
 *
 */
public class ErrorDataCheckOut {
	static final String fileFolder = "D:\\data\\errorData\\errorData";
	static final String outFile = "D:\\data\\errorData\\totalErrorData.csv";
	static final String errorRoadfileFolder = "C:\\Users\\NXZ\\Desktop\\yanzheng1\\";
	public static void main(String[] args) throws IOException {
		// TODO Auto-generated method stub
		//汇总所有小文件,只运行一次即可
//		getAllErrorData(fileFolder,outFile);
		//生成mapinfo展示的工具
		getMapInfoShowData(outFile,errorRoadfileFolder);

	}
	private static void getMapInfoShowData(String inputFile, String outFloder) throws IOException {
		// TODO Auto-generated method stub
		BufferedReader br=new BufferedReader(new FileReader(new File(inputFile)));
		String line="";
		while((line=br.readLine())!=null){
			String [] lineStr=line.split("#");
			String trajDataStr=lineStr[0];
			String bufferPoint=lineStr[1];
			String roadId=lineStr[2].replace(",null)", "").replace("|", "_");
			String outFileName=outFloder+roadId;
			String outFile=outFileName+".txt";
			
			for(int i=0;i<100;i++){
				String outFileName1=outFileName+"#"+i;
				File name=new File(outFileName1+"_.txt");
				if(!name.exists()){
					BufferedWriter write=new BufferedWriter(new FileWriter(new File(outFileName1+"_.txt")));
//					write.newLine();
//					write.flush();
					String [] trajArr=trajDataStr.replace("(ERROR:", "").replace(")", "").replace("(", "").split("\\|");
					String [] bufferArr=bufferPoint.replace(",null)", "").replace(")", "").replace("(", "").split("\\|");
					
					for(String teajPoint:trajArr){
						String []arr=teajPoint.split(",");
						System.out.println(arr[1]+" "+arr[2]+" "+arr[3]);
						write.write(arr[1]+" "+arr[2]+" "+arr[3]);
						write.newLine();
						write.flush();
					}
					for(String bufPoint:bufferArr){
						System.out.println(bufPoint+" buffer");
						write.write(bufPoint+" buffer");
						write.newLine();
						write.flush();
					}
					write.close();
					write=null;
					break;
				}
			}
			
		}
	}
	private static void getAllErrorData(String fileFolder, String outFile) throws IOException {
		// TODO Auto-generated method stub
		BufferedWriter out=new BufferedWriter(new FileWriter(new File(outFile)));
		File[]files=new File(fileFolder).listFiles();
		for(File file:files){
			File[]filesC=file.listFiles();
			for(File fileC:filesC){
				if (fileC.getName().contains("part")){
					BufferedReader br=new BufferedReader(new FileReader(fileC));
					String line="";
					while((line=br.readLine())!=null){
						if(line.contains("LOC_GPS")){
							out.write(line);
							out.newLine();
							out.flush();
						}
						
					}
				}
			}
		}
		out.close();
	}

}
